package com.woniuxy.usercoachassemble.commons;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: SMSUtil
 * Description: 短信工具类
 * date: 2020/7/10 9:57
 *
 * @author Fy浪迹、
 */
public class SMSUtils {

    public static final String ACCESS_KEY_ID = "LTAI4GGdcvA3t4L6CZYVNTGn";
    public static final String ACCESS_KEY_SECRET = "iNDC6nFmIf3Gkkt3UuLcjCzpeOROvK";

    // 登录模板code
    public static final String TEMPLATECODE_LOGIN = "SMS_195721820";
    // 注册模板code
    public static final String TEMPLATECODE_REGISTER = "SMS_195871036";
    // 找回密码模板code
    public static final String TEMPLATECODE_RETRIEVE_PASSWORD = "SMS_196145127";



    /**
     * @description:TODO(发送短信)
     * @param tel 接受短信人电话
     * @param templateCode 模板
     * @param code 验证码
     * @return:  boolean 发送短信成功: true 失败:false
     * @author: Fy浪迹、
     * @date: 2020/7/10 10:48
     */
    public static boolean sendSMS(String tel,String templateCode,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "瑜伽产品商城");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+ code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());

            Gson gson = new Gson();
            Map<String, String> map = new HashMap<String, String>();
            map = gson.fromJson(response.getData(), map.getClass());
            if(map.get("Code").equals("OK")){
                return true;
            }

        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

        return false;
    }

}
